import { BasicColumn } from '@/components/Table';
import { DescItem } from '@/components/Description';
import { handleAddrText } from '@/hooks/web/useCommon';
import { deliveryMethodList } from '@/enums/productionEnum';
import { FormActionType, FormSchema } from '@/components/Form';

export const tableFormSchema: FormSchema[] = [
  {
    field: 'orderCode',
    label: '销售单号',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'orderTime',
    label: '下单时间',
    component: 'RangePicker',
    colProps: { span: 6 },
    componentProps: {
      style: { width: '100%' },
      format: 'YYYY-MM-DD HH:mm:ss.SSS',
    },
  },
  {
    field: 'orderPlacerUserPhone',
    label: '下单手机号',
    component: 'Input',
    colProps: { span: 6 },
  },
];

export const orderItemList = [
  { label: '销售单号', value: 'orderCode' },
  { label: '下单时间', value: 'orderTime' },
  { label: '收款时间', value: 'paidTime' },
  { label: '结算方式', value: 'paymentType', defaultValue: '全款' },
  { label: '订单总金额（元）', value: 'settlementAmount' },
  { label: '支付方式', value: 'type', defaultValue: '微信' },
  { label: '实收金额（元）', value: 'paidAmount' },
  { label: '未付金额（元）', value: 'unpaidAmount' },
  { label: '下单手机号', value: 'orderPlacerUserPhone' },
  {
    label: '送货方式',
    value: 'deliveryMethod',
    customRender: (val: any) => {
      return deliveryMethodList.find((find) => find.value === val)?.label;
    },
  },
  { label: '推荐人名称', value: 'referrerUserName' },
];

export const collapsePanelSchema = [
  { label: '订单信息', key: 'order' },
  { label: '产品信息', key: 'product' },
  { label: '结算信息', key: 'settlement' },
  { label: '物流信息', key: 'logistics' },
];

// 订单信息
export const orderDescSchema: DescItem[] = [
  { field: 'orderCode', label: '订单编号' },
  { field: 'orderPlacerUser', label: '下单人' },
  { field: 'orderPlacerUserPhone', label: '下单人电话' },
  { field: 'orderId', label: '订单系统标识' },
  { field: 'orderTime', label: '下单时间' },
  { field: 'paidTime', label: '支付时间' },
];

// 产品信息
export const productTableColumns: BasicColumn[] = [
  { width: 180, title: '产品单号', dataIndex: 'orderItemId' },
  { width: 150, title: '产品名称', dataIndex: 'spuName' },
  { width: 150, title: '展开尺寸（mm）', dataIndex: 'productMeasure' },
  { width: 120, title: '数量', dataIndex: 'quantity' },
  { width: 120, title: '单价', dataIndex: 'unitSettlementPrice' },
  { title: '工艺信息', dataIndex: 'productDesc' },
  {
    width: 100,
    title: '图片',
    dataIndex: 'bluePrintImgUrl',
    slots: { customRender: 'bluePrintImgUrl' },
  },
  { width: 120, title: '产品文件', dataIndex: 'attachment', slots: { customRender: 'attachment' } },
];

// 结算信息
export const settlementDescSchema: DescItem[] = [
  { field: 'paymentType', label: '结算方式' },
  { field: 'sellingAmount', label: '订单销售金额（元）' },
  { field: 'adjustmentAmount', label: '优惠金额（元）' },
  { field: 'invoice', label: '是否开票' },
  { field: 'taxAmount', label: '税金（元）' },
  { field: 'settlementAmount', label: '实收金额（元）' },
];

// 物流信息
export const logisticsTableColumns: BasicColumn[] = [
  {
    title: '运输方式',
    dataIndex: 'deliveryMethod',
    customRender({ text }) {
      return deliveryMethodList.find((find) => find.value === text)?.label;
    },
  },
  { title: '物流公司', dataIndex: 'logisticsCompanyName' },
  { title: '物流单号', dataIndex: 'logisticsTrackingNumber' },
  { title: '发货日期', dataIndex: 'deliveryTime' },
  {
    title: '收货地址',
    dataIndex: 'address',
    customRender: ({ record }) => {
      return handleAddrText(record);
    },
  },
  { title: '物流单图片', dataIndex: 'billImageList', slots: { customRender: 'billImageList' } },
  { title: '备注', dataIndex: 'remark' },
];

async function handleResultAmount(formModel: Recordable, formActionType: FormActionType) {
  const { settlementAmount, adjustmentMethod, adjustmentAmount: amount } = formModel;
  const ifIncrease = adjustmentMethod === 1;
  const { setFieldsValue } = formActionType;
  await setFieldsValue({
    resultAmount: (settlementAmount - (amount ? (ifIncrease ? -amount : amount) : 0)).toFixed(2),
  });
}
export const changePriceFormSchema: FormSchema[] = [
  {
    ifShow: false,
    field: 'orderItemId',
    label: '订单项id',
    component: 'InputNumber',
  },
  {
    field: 'settlementAmount',
    label: '商品金额',
    component: 'InputNumber',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'adjustmentMethod',
    label: '调整方式',
    component: 'Select',
    componentProps: ({ formModel, formActionType }) => {
      return {
        options: [
          { label: '增加', value: 1 },
          { label: '减少', value: 2 },
        ],
        onChange: async () => {
          await handleResultAmount(formModel, formActionType);
        },
      };
    },
  },
  {
    required: true,
    field: 'adjustmentAmount',
    label: '调整金额',
    component: 'InputNumber',
    componentProps: ({ formModel, formActionType }) => {
      return {
        min: 0,
        onChange: async () => {
          await handleResultAmount(formModel, formActionType);
        },
      };
    },
  },
  {
    field: 'resultAmount',
    label: '调整后金额',
    component: 'InputNumber',
    componentProps: {
      disabled: true,
    },
  },
];
